# -*- coding: utf-8 -*-
"""
 ----------------------------------------
|File Name:     overWriteMultipartEncoder
|Author:        WYT
|date:          2021/6/11
 ----------------------------------------
|  Change Activity:
|                   2021/6/11:
-----------------------------------------
"""
from requests_toolbelt import MultipartEncoder
from requests_toolbelt._compat import fields


class overWriteMultipartEncoder(MultipartEncoder):
    def _iter_fields(self):
        _fields = self.fields
        if hasattr(self.fields, 'items'):
            _fields = list(self.fields.items())
        for k, v in _fields:
            file_name = None
            file_type = None
            file_headers = None
            if isinstance(v, (tuple, )):
                if len(v) == 2:
                    file_name, file_pointer = v
                elif len(v) == 3:
                    file_name, file_pointer, file_type = v
                else:
                    file_name, file_pointer, file_type, file_headers = v
            else:
                file_pointer = v

            field = fields.RequestField(name=k, data=file_pointer,
                                        filename=file_name,
                                        headers=file_headers)
            field.make_multipart(content_type=file_type)
            yield field


if __name__ == '__main__':
     data = {'dataEprProject.id': '', 'dataInnocom.id': '', 'dataInnocomId': '', 'dataEprProject.dataId': '',
     'dataEprProject.entId': '', 'dataEprProject.createDate': '', 'struts.token.name': 'token',
     'token': 'KKSEAF3VI5KEVKEXE21HQSKWZPZBXCGB', 'dataEprProject.Pxmbh': '01',
     'dataEprProject.Pxmmc': '燃煤电厂宽温度窗口SCR脱硝催化剂关键技术应用研究RD01', 'dataEprProject.Pqzsj': '2018-01-01',
     'dataEprProject.Pjssj': '2018-12-31', 'dataEprProject.Pjslya': '6', 'dataEprProject.Pzscqbha': ['IP01', 'IP03'],
     '__multiselect_dataEprProject.Pzscqbha': '', 'dataEprProject.Pyfjfzys': '5.0', 'dataEprProject.Pyfjfzzc': '173.5',
     'dataEprProject.Pyfzc3': '173.5', 'dataEprProject.Pyfzc2': '0.0', 'dataEprProject.Pyfzc1': '0.0',
     'dataEprProject.Plxmd': '1', 'dataEprProject.Phxjs': '1', 'dataEprProject.Pqdcg': '1',
     'dataEprProject.Pjsly1': '1', 'dataEprProject.Pjsly2': '10', 'dataEprProject.Pjsly3': '18'}

     data = overWriteMultipartEncoder(fields=data)
     print(data)
